package com.treasure.mall.biz.service.core.live.impl;

import com.treasure.mall.biz.base.AbstractCrudService;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.live.DirectorSaveDTO;
import com.treasure.mall.biz.entity.live.LiveDirectorPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.repository.live.LiveDirectorRepository;
import com.treasure.mall.biz.service.core.live.LiveDirectorService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.base.ResultCodeEnum;
import com.treasure.mall.zbq.common.exception.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/6/13
 */
@Service
public class LiveDirectorServiceImpl extends AbstractCrudService<LiveDirectorPO, Long> implements LiveDirectorService {
    private LiveDirectorRepository directorRepository;

    @Autowired
    protected LiveDirectorServiceImpl(LiveDirectorRepository repository) {
        super(repository);
        this.directorRepository = repository;
    }

    @Override
    @Transactional
    public LiveDirectorPO save(BaseSaveDTO saveDTO) {
        DirectorSaveDTO directorSaveDTO = (DirectorSaveDTO) saveDTO;

        LiveDirectorPO liveDirectorPO = findByRoomAndUser(directorSaveDTO.getRoomId(), directorSaveDTO.getUserId());
        if (liveDirectorPO != null && !liveDirectorPO.isDeleted()) {
            throw new ApiException(ApiResult.errorWith(ResultCodeEnum.ILLEGAL_STATE, "当前主播已经存在"));
        }
        if (liveDirectorPO == null) {
            liveDirectorPO = new LiveDirectorPO();
        }
        liveDirectorPO.setDeleted(false);
        liveDirectorPO.setRoomId(directorSaveDTO.getRoomId());
        liveDirectorPO.setOwner(directorSaveDTO.isOwner());
        liveDirectorPO.setUserPO(new UserPO(directorSaveDTO.getUserId()));
        liveDirectorPO.setDirectorName(directorSaveDTO.getDirectorName());
        return save(liveDirectorPO);
    }

    @Override
    public List<LiveDirectorPO> findByRoom(Long roomId) {
        return directorRepository.findByRoomIdAndDeletedIsFalse(roomId);
    }

    @Override
    public LiveDirectorPO findByRoomAndUser(Long roomId, Long userId) {
        return directorRepository.findByRoomIdAndUserPO_UserId(roomId, userId);
    }

    @Override
    public void modifyName(Long id, String directorName) {
        LiveDirectorPO directorPO = findById(id);
        directorPO.setDirectorName(directorName);

        save(directorPO);
    }

    /**
     * 获取用户授权的直播间ID
     *
     * @param userId
     * @return
     */
    @Override
    public List<Long> findRoomIdsByUserId(Long userId) {
        List<LiveDirectorPO> pos = directorRepository.findByUserId(userId);
        if (pos == null || pos.size() == 0) {
            return null;
        }
        List<Long> roomIds = new ArrayList<>();
        for (LiveDirectorPO po : pos) {
            roomIds.add(po.getRoomId());
        }
        return roomIds;
    }

    /**
     * 是否有授权直播间
     *
     * @param userId
     * @return
     */
    @Override
    public boolean checkIsAuthLive(Long userId) {
        long count = directorRepository.countByUserPO_UserIdAndOwnerIsFalseAndDeletedIsFalse(userId);
        return count > 0;
    }


    @Override
    public void deleteById(Long id) {
        directorRepository.delete(id);
    }

}
